<?php
session_start();
header('Content-type:text/html;charset=utf-8');
$id=$_GET["id"];
if(isset($_SESSION['user']) && !empty($_SESSION['user'])):
    $user = $_SESSION['user'];
    $link = mysqli_connect("localhost","root","123",'Dress', '3306');
    mysqli_query($link,"set names 'utf8'");
    $sql = "select * from goods where name = '$id'";
    $result = mysqli_query($link,$sql);
    $row = mysqli_fetch_array($result);
    $name =  $row[0];
    $price =  $row[1];
    $pic = $row[2];
    $sql_insert = "insert into shopcard (product,num,price,pic,username) values('$name','1','$price','$pic','$user')";
    $res_insert = mysqli_query($link,$sql_insert);
    if ($res_insert) {
        echo "<script>alert('添加成功！');history.go(-1); </script>";
        mysqli_free_result($result);
    } else {
        echo "<script>alert('购物车已存在！');history.go(-1); </script>";
    }
    else:echo "<script>alert('请先登录！');  history.go(-1);</script>";
    endif;
    ?>